################################################################################
### Replication Code for Appendix F                                          ###
### Title: Do External Threats Increase Bipartisanship in the United States? ###
###        An Experimental Test in the Shadow of China's Rise                ###
### Authors: Eddy S. F. Yeung, Weifang Xu                                    ###
### Version: September 24, 2024                                              ###
################################################################################

### Set-up ----
## Clean the working environment and set up the working directory
rm(list = ls())
# setwd()  # set your working directory here, which should also contain the survey data ("survey_data.csv") and Myrick's dataset ("myrick_external_threat_data_study3.csv")

## Load the required packages
library(tidyverse)
library(estimatr)

## Import our dataset
df <- read.csv("survey_data.csv")

### Recoding variables ----
## Indicators of treatment status
table(df$exp_3) 
df <- df %>% mutate(exp_condition = case_when(
  exp_3 == 1 ~ "No Threat Prime",
  exp_3 == 2 ~ "Biden Threat Prime",
  exp_3 == 3 ~ "Trump Threat Prime",
  exp_3 == 4 ~ "Nonpartisan Threat Prime"
))
df$exp_condition <- factor(df$exp_condition,
                           levels = c("No Threat Prime", "Biden Threat Prime", 
                                      "Trump Threat Prime", "Nonpartisan Threat Prime"))
table(df$exp_condition)

## Partisanship (-3 = strong Democrat; -3 = strong Republican)
df <- df %>% mutate(pid7 = case_when(
  pid_1 == 2 & pid_2d == 1 ~ -3,
  pid_1 == 2 & pid_2d == 2 ~ -2,
  (pid_1 == 3 | pid_1 == 4) & pid_2i == 2 ~ -1,
  (pid_1 == 3 | pid_1 == 4) & pid_2i == 4 ~ 0,
  (pid_1 == 3 | pid_1 == 4) & pid_2i == 1 ~ 1,
  pid_1 == 1 & pid_2r == 2 ~ 2,
  pid_1 == 1 & pid_2r == 1 ~ 3
))
table(df$pid7)

df <- df %>% mutate(pid3 = case_when(
  pid7 ==  0 ~ "Independent",
  pid7 == -3 | pid7 == -2 | pid7 == -1 ~ "Democrat",
  pid7 ==  3 | pid7 ==  2 | pid7 ==  1 ~ "Republican"
))
table(df$pid3)
df$pid3 <- factor(df$pid3,
                  levels = c("Independent", "Democrat", "Republican"))
table(df$pid3)

### Figure S16: Mean and Distribution of Threat Perceptions of China by Partisanship and Experimental Condition in Our Survey ----
## Perceived China threat among Democrats
summary_temp_dem <- df %>% 
  subset(pid3 == "Democrat") %>% 
  group_by(exp_condition) %>%
  do(tidy(lm_robust(china_threat ~ 1, data = .))) %>% 
  mutate(china_threat = estimate, partisanship = "Democrat")

## Perceived China threat among Republicans
summary_temp_gop <- df %>% 
  subset(pid3 == "Republican") %>% 
  group_by(exp_condition) %>%
  do(tidy(lm_robust(china_threat ~ 1, data = .))) %>% 
  mutate(china_threat = estimate, partisanship = "Republican")

## Merge the estimates
summary_temp <- bind_rows(summary_temp_dem, summary_temp_gop)

ggplot(summary_temp, aes(x = exp_condition, y = china_threat)) + 
  geom_jitter(data = subset(df, pid3 == "Democrat"), 
              size = 1, alpha = 0.05, na.rm = T, color = "#000099", 
              position = position_jitter(width = 0.1, height = 0.2)) +
  geom_jitter(data = subset(df, pid3 == "Republican"), 
              size = 1, alpha = 0.05, na.rm = T, color = "#990000", 
              position = position_jitter(width = 0.1, height = 0.2)) +
  geom_point(aes(color = partisanship, shape = partisanship), 
             size = 2, position = position_dodge(0.15)) +
  scale_color_manual(values = c("#000099", "#990000")) +
  scale_shape_manual(values = c(19, 15)) +
  geom_errorbar(aes(color = partisanship, ymin = conf.low, ymax = conf.high),
                linewidth = 0.5, width = 0, position = position_dodge(.15)) +
  theme_bw() +
  xlab("") +
  ylab("Perceived China Threat") +
  coord_cartesian(ylim = c(0.85, 7.15)) +
  theme(text = element_text(color = "black", family = "Times", size = 12),
        axis.text = element_text(color = "black", family = "Times", size = 12),
        legend.justification = c(1, 1), legend.position = c(1, .12),
        legend.background = element_rect(fill = "transparent"),
        legend.box.background = element_rect(color = NA, fill = "transparent"), 
        legend.key = element_rect(color = "transparent", fill = "transparent"),
        legend.key.size = unit(1, "line"),
        legend.direction = "vertical",
        legend.margin = margin(t = 0, r = 0.2, b = 0.2, l = 0.2, unit = "cm"),
        legend.title = element_blank())

ggsave("china_threat_2023.pdf", width = 8, height = 5)

### Figure S17: Mean and Distribution of Threat Perceptions of China by Partisanship and Experimental Condition in Myrick’s Survey ----
## Read Myrick's dataset (downloaded from https://doi.org/10.7910/DVN/YNVYO2)
repli <- read.csv("myrick_external_threat_data_study3.csv")
repli$frame <- 
  factor(repli$frame,
         levels = c("Control", "Partisan", "Non-Partisan"),
         labels = c("No Threat Prime", "Trump Threat Prime", "Nonpartisan Threat Prime"))

## Perceived China threat among Democrats
summary_temp_dem_repli <- repli %>% 
  subset(pid1_l == "Democrat") %>% 
  group_by(frame) %>%
  do(tidy(lm_robust(china_threat_scale ~ 1, data = .))) %>% 
  mutate(china_threat_scale = estimate, partisanship = "Democrat")

## Perceived China threat among Republicans
summary_temp_gop_repli <- repli %>% 
  subset(pid1_l == "Republican") %>% 
  group_by(frame) %>%
  do(tidy(lm_robust(china_threat_scale ~ 1, data = .))) %>% 
  mutate(china_threat_scale = estimate, partisanship = "Republican")

## Merge the estimates
summary_temp_repli <- bind_rows(summary_temp_dem_repli, summary_temp_gop_repli)

ggplot(summary_temp_repli, aes(x = frame, y = china_threat_scale)) + 
  geom_jitter(data = subset(repli, pid1 == "Democrat"), 
              size = 1, alpha = 0.05, na.rm = T, color = "#000099", 
              position = position_jitter(width = 0.07, height = 0.2)) +
  geom_jitter(data = subset(repli, pid1 == "Republican"), 
              size = 1, alpha = 0.05, na.rm = T, color = "#990000", 
              position = position_jitter(width = 0.07, height = 0.2)) +
  geom_point(aes(color = partisanship, shape = partisanship), 
             size = 2, position = position_dodge(0.15)) +
  scale_color_manual(values = c("#000099", "#990000")) +
  scale_shape_manual(values = c(19, 15)) +
  geom_errorbar(aes(color = partisanship, ymin = conf.low, ymax = conf.high),
                linewidth = 0.5, width = 0, position = position_dodge(0.15)) +
  theme_bw() +
  xlab("") +
  ylab("Perceived China Threat") +
  coord_cartesian(ylim = c(0.85, 7.15)) +
  theme(text = element_text(color = "black", family = "Times", size = 12),
        axis.text = element_text(color = "black", family = "Times", size = 12),
        legend.justification = c(1, 1), legend.position = c(1, .12),
        legend.background = element_rect(fill = "transparent"),
        legend.box.background = element_rect(color = NA, fill = "transparent"), 
        legend.key = element_rect(color = "transparent", fill = "transparent"),
        legend.key.size = unit(1, "line"),
        legend.direction = "vertical",
        legend.margin = margin(t = 0, r = 0.2, b = 0.2, l = 0.2, unit = "cm"),
        legend.title = element_blank())

ggsave("china_threat_2019.pdf", width = 8, height = 5)
